/*
 * Copyright (c) JoinQuant Development Team
 * Author: Huayong Kuang <kuanghuayong@joinquant.com>
 */

#include <iostream>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <unistd.h>

using namespace std;

bool getCurrentTime() {
    char buf[15], buf2[20];
    struct timeval tv;
    int millisec;
    time_t curtime;

    gettimeofday(&tv, NULL);

    millisec = lrint(tv.tv_usec / 1000.0);  // Round to nearest millisec
    if (millisec >= 1000) {  // Allow for rounding up to nearest second
        cout << "------> " << tv.tv_usec << " " << millisec << endl;
        millisec -= 1000;
        tv.tv_sec++;
    }

    curtime = tv.tv_sec;
    strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&curtime));
    sprintf(buf2, "%s.%03d", buf, millisec);

    char at9[3] = "09";
	if (strcmp(buf2, at9) < 0) {
		cout << "time: " << buf2 << endl;
		return false;
	} else {
		return true;
	}
}

int main(void)
{
	while (true) {
		bool ret = getCurrentTime();
		if (!ret) {
            break;
		}
        usleep(10);
	}

    return 0;
}

